package com.android.mios.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.android.mios.model.Engine;
import com.android.mios.model.Engine.ForwardServer;
import android.content.Context;

public class ParseData {

	private static ParseData _instance;

	private Context mContext;

	public static ParseData getInstance(Context context) {

		if (_instance == null)
			_instance = new ParseData(context);

		return _instance;
	}

	public static ParseData getInstance() {

		return _instance;
	}

	private ParseData(Context context) {

		this.mContext = context;
	}

	public Map<Integer, Engine> parseEngines(JSONObject json) {

		Map<Integer, Engine> engineList = null;
		if (json != null) {
			engineList = new LinkedHashMap<Integer, Engine>();
			JSONArray units = json.optJSONArray("units");
			if (units != null) {
				for (int i = 0; i < units.length(); i++) {
					JSONObject engineObject = (JSONObject) units.opt(i);
					Engine engine = parseSubEngine(engineObject);
					engineList.put(engine.getSerialNumber(), engine);
				}
			}

			JSONArray accesories = json.optJSONArray("accesories");
			if (accesories != null) {
				for (int i = 0; i < accesories.length(); i++) {
					JSONObject engineObject = (JSONObject) accesories.opt(i);
					Engine engine = parseSubEngine(engineObject);
					engine.setAccessories(true);
					if (engineList.containsKey(engine.getSerialNumber())) {
						engine.setSerialNumber(engine.getSerialNumber() + 1);
					}
					engineList.put(engine.getSerialNumber(), engine);
				}
			}
		}

		return engineList;
	}

	private Engine parseSubEngine(JSONObject json) {

		Engine engine = new Engine();
		Iterator<String> keys = json.keys();
		Map<String, String> settings = new HashMap<String, String>();
		while (keys.hasNext()) {
			String key = keys.next();
			if ("active_server".equalsIgnoreCase(key)) {
				engine.setActiveServer(json.optString(key));
			} else if ("FirmwareVersion".equalsIgnoreCase(key)) {
				engine.setFirmwareVersion(json.optString(key));
			} else if ("forwardServers".equalsIgnoreCase(key)) {
				JSONArray forwardServerObjects = json
						.optJSONArray("forwardServers");
				if (forwardServerObjects != null) {
					List<ForwardServer> forwardServers = new ArrayList<ForwardServer>();
					for (int i = 0, n = forwardServerObjects.length(); i < n; i++) {
						JSONObject forwardServerObject = (JSONObject) forwardServerObjects
								.opt(i);
						if (forwardServerObject != null) {
							ForwardServer forwardServer = new ForwardServer();
							forwardServer.setHostName(forwardServerObject
									.optString("hostName"));
							forwardServer.setPrimary(forwardServerObject
									.optBoolean("primary"));
							forwardServers.add(forwardServer);
						}
					}
					engine.setForwardServers(forwardServers);
				}
			} else if ("ipAddress".equalsIgnoreCase(key)) {
				engine.setIpAddress(json.optString(key));
			} else if ("name".equalsIgnoreCase(key)) {
				engine.setName(json.optString(key));
			} else if ("serialNumber".equalsIgnoreCase(key)) {
				engine.setSerialNumber(json.optInt(key));
			} else if ("model".equalsIgnoreCase(key)) {
//				engine.setModel(json.optString(key));
			} else if ("users".equalsIgnoreCase(key)) {
				JSONArray userObjects = json.optJSONArray(key);
				if (userObjects != null) {
					List<String> users = new ArrayList<String>();
					for (int i = 0, n = userObjects.length(); i < n; i++) {
						Object userObject = userObjects.opt(i);
						users.add(userObject.toString());
					}
					engine.setUsers(users);
				}
			} else if ("category".equalsIgnoreCase(key)) {
				engine.setCategory(json.optInt(key));
			} else if ("subcategory".equalsIgnoreCase(key)) {
				engine.setSubCategory(json.optInt(key));
			} else if ("streaming".equalsIgnoreCase(key)) {
				engine.setStreaming(json.optString(key));
			} else if ("url".equalsIgnoreCase(key)) {
				engine.setUrl(json.optString(key));
			} else if ("units".equalsIgnoreCase(key)) {
				JSONArray unitObjects = json.optJSONArray(key);
				if (unitObjects != null) {
					List<Integer> units = new ArrayList<Integer>();
					for (int i = 0, n = unitObjects.length(); i < n; i++) {
						JSONObject unitObject = unitObjects.optJSONObject(i);
						if (unitObject != null) {
							int unit = unitObject.optInt("serialNumber");
							units.add(unit);
						}
					}
					engine.setUnits(units);
				}
			} else if ("local_remote".equalsIgnoreCase(key)) {
				engine.setRemote(json.optInt(key));
			} else if ("relay_remote".equalsIgnoreCase(key)) {
				engine.setRemote(json.optInt(key));
			} else {
				settings.put(key, json.optString(key));
			}
		}
		engine.setSettings(settings);
		return engine;
	}

}
